class Solution978 {
    public static void main(String[] args) {
        int[] arr = {37,199,60,296,257,248,115,31,273,176};
        System.out.println(maxTurbulenceSize(arr));
    }
    public static int maxTurbulenceSize(int[] arr) {
        if(arr.length <= 1) return arr.length ;
        if(arr.length == 2) return arr[0] == arr[1] ? 1 : 2 ;
        int n = arr.length ;
        // 以i结尾, 最长的湍流子数组的长度 ;
        int[] dp = new int[n] ;
        int prev = arr[1] - arr[0] ;
        dp[0] = 1 ; dp[1] = 2 ;
        if(arr[1] == arr[0]) dp[1]=1 ;
        int max = 1 ;
        for(int i=2 ; i < n ; i ++){
            int cur = arr[i] - arr[i-1] ;
            if(cur > 0){
                if(prev < 0){
                    dp[i] = dp[i-1] + 1 ;
                }else{
                    dp[i] = 2 ;
                }
            }else if(cur < 0 ){
                if(prev > 0){
                    dp[i] = dp[i-1] + 1 ;
                }else{
                    dp[i] = 2 ;
                }
            }else{
                dp[i] = 1 ;
            }
            prev = cur ;
            max = Math.max(max , dp[i]) ;
        }

        return max ;
    }
}